package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.form.Add2CartForm;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.form.UpdateCartForm;
import com.cskaoyan.shopping.service.ICartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shopping")
public class CartsController {

    @Autowired
    ICartService iCartService;


    /**
     * TODO: 2022/10/25 获得购物车列表
     *
     * @param
     * @return
     */
    @GetMapping("carts")
    public ResponseData getCartList(HttpServletRequest request) {

        //拿到请求体中存放的用户信息
        String user_info = request.getHeader("user_info");
        JSONObject object = JSON.parseObject(user_info);

        //获取userId
        long userId = Long.parseLong(object.get("uid").toString());

        List<CartsProductDto> cartList = iCartService.getCartListById(userId);


        return new ResponseUtil().setData(cartList);
    }

    /**
     * 添加购物车列表
     * {
     * "userId": "74",
     * "productId": 100053312,
     * "productNum": 1
     * }
     * <p>
     * {
     * "success": true,
     * "message": null,
     * "code": 200,
     * "result": null,
     * "timestamp": 1666699102055
     * }
     *
     * @param add2Cart 添加购物车列表
     * @return {@link ResponseData}
     */

    @PostMapping("carts")
    public ResponseData addToCart(@RequestBody Add2CartForm add2Cart) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(add2Cart.getUserId());
        addCartRequest.setItemId(add2Cart.getProductId());
        addCartRequest.setNum(add2Cart.getProductNum());
        AddCartResponse response = iCartService.addToCart(addCartRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody UpdateCartForm updateCartForm) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(updateCartForm.getUserId());
        request.setItemId(updateCartForm.getProductId());
        request.setNum(updateCartForm.getProductNum());
        request.setChecked(updateCartForm.getChecked());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleleCart(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setItemId(pid);
        request.setUserId(uid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckCart(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResponse response = iCartService.deleteCheckedItem(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }


    /**
     * 请求网址: http://localhost:8080/shopping/items
     * 全选/全不选购物车
     *
     * @return
     */
    @PutMapping("items")
    public ResponseData checkAllCart(@RequestBody CheckAllItemRequest request) {
        //全选/全不选


        iCartService.checkAllCartItem(request);

        return new ResponseUtil().setData(null);


    }

}
